/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.prep;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/**
 * A class representing a graphics model.
 * For example:
 * <pre>
 *    Model model = new Model();
 *    model.putNode();
 * </pre>
 *
 * @author Pitangueira, Roque & Brugiolo, Marco & Flavio, Fonseca
 * @version 0.0.1 Beta  
 * @see     gui.Interface
 * @see     gui.DrawingArea
 * @since   jdk1.4.1
 */
public class MappingModel implements Serializable {
	
    private static final long serialVersionUID = 1L;
	private List<MapNode> mapNodes = new ArrayList<MapNode>();
    private List<MapElement> mapElements = new ArrayList<MapElement>();
    
//*****************************************************************************
	
	public void add(MapNode nd) {
		mapNodes.add(nd);
	}
	
//*****************************************************************************
	
	public void remove(MapNode nd) {
		MapNode nod;
		
		ListIterator nodes = mapNodes.listIterator();
		while (nodes.hasNext()) {
			nod = (MapNode)nodes.next();
			if (nod.equals(nd)){
//				mapNodes.remove();
				nodes.remove();
			}
		}
	}
    
//*****************************************************************************
	
	public void removeMapNode(int index) {
		mapNodes.remove(index);
	}
    
//*****************************************************************************
	
	public MapNode getMapNode(int index) {
		return (MapNode)mapNodes.get(index);
	}
	
//*****************************************************************************
	
	/** Returns the MapNode with the specified coords.
	*@param x The coord X of the desired MapNode.
	*@param y The coord Y of the desired MapNode.
	*@return The MapNode with the specified coords.
	*/
	public MapNode getMapNode(double x, double y) {
		MapNode node;
		ListIterator nds = mapNodes.listIterator();
		while (nds.hasNext()) {
			node = (MapNode) nds.next();
			if (node.getX() == x && node.getY() == y){
				return (node);
			};
		};
		return (null);
	};
	
//*****************************************************************************
	
    public List<MapNode> getMapNodes() {
        return mapNodes;
    }
	
//*****************************************************************************
	
    public void setMapNodes(List<MapNode> newNodes) {
		
        ListIterator onds = mapNodes.listIterator();
        ListIterator nnds = newNodes.listIterator();
        while (nnds.hasNext()) {
            MapNode nnd = (MapNode)nnds.next();
            MapNode ond = (MapNode)onds.next();
            int index = mapNodes.indexOf(ond);
            if ( (nnd.getLabel())!=(ond.getLabel()) )
            	mapNodes.set(index, nnd);
        }
		
        mapNodes = newNodes;
    }
    
//*****************************************************************************
	
    public int indexOf(MapNode nd) {
        int index = -1;
        MapNode nod;
        ListIterator nds = mapNodes.listIterator();
        while (nds.hasNext()) {
            nod = (MapNode)nds.next();
            if (nod.equals(nd)){
                index = mapNodes.indexOf(nod);
            }
        }
        return index;
    }
    
//*****************************************************************************
	
    public void clearMapNodes() {
        mapNodes.clear();
    }
	
//*****************************************************************************
	
	public void add(MapElement el) {
		mapElements.add(el);
	}
	
//*****************************************************************************
	
	public void remove(MapElement el) {
		MapElement ele;
		int n1 = el.getLabel();
		
		ListIterator elements = mapElements.listIterator();
		while (elements.hasNext()) {
			ele = (MapElement)elements.next();
			int n2 = ele.getLabel();
			if (n1 == n2){
				elements.remove();
			}
		}
	}
    
//*****************************************************************************
	
	public void removeMapElement(int index) {
		mapElements.remove(index);
	}
    
//*****************************************************************************
	
	public MapElement getMapElement(int index) {
		return (MapElement)mapElements.get(index);
	}
	
//*****************************************************************************
	
    public List<MapElement> getMapElements() {
        return mapElements;
    }
    
//*****************************************************************************
	
    public int indexOf(MapElement el) {
        int index = -1;
        MapElement ele;
        ListIterator els = mapElements.listIterator();
        while (els.hasNext()) {
            ele = (MapElement)els.next();
            if (ele.equals(el)){
                index = mapElements.indexOf(ele);
            }
        }
        return index;
    }
    
//*****************************************************************************
	
    public void clearMapElements() {
        mapElements.clear();
    }
	
//*****************************************************************************
    
    public void clear() {
        mapNodes.clear();
        mapElements.clear();
    } 
	
//*****************************************************************************
}
